package com.badibadi.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import com.badibadi.adapter.AfterLandingMyHomePageAllAdapter_1;
import com.badibadi.infos.Activity_Overview_Model;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.libjph.custom.xlistview.XListView;
import com.libjph.custom.xlistview.XListView.XListViewListener;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Data_Me_Activity extends BaseActivity implements
		XListViewListener, OnClickListener {

	private String TAG = "Data_Me_Activity";
	private XListView xListView;
	private Button data_during_btn_back, data_during_btn_index;
	private TextView data_during_tital;
	private AfterLandingMyHomePageAllAdapter_1 x1Adapter;

	private List<Activity_Overview_Model> Models;
	private String timedata = null;
	private String uid = "1";
	private String fid = "-1";
	private String cid = null;
	// List<Activity_Overview_Model> overview_Models;
	List<Activity_Overview_Model> list;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(Data_Me_Activity.this);
				try {
					Utils.showMessage(Data_Me_Activity.this, getResources()
							.getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 2:
//				   
				try {
					Utils.ExitPrgress(Data_Me_Activity.this);
					Models.addAll(list);
					AddData();
				} catch (Exception e) {
					// TODO: handle exception
				}

				// Intent intent = new Intent(Data_Me_Activity.this,
				// CalendarBetweenActivity.class);
				// intent.putExtra("搜索内容", autocompleteTextview.getText()
				// .toString().trim());
				// startActivity(intent);
				break;
			case 3:
				Utils.ExitPrgress(Data_Me_Activity.this);
				try {
					Utils.showMessage(Data_Me_Activity.this, getResources()
							.getString(R.string.l_xa10));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		goToTheTitle();
		setContentView(R.layout.data_during_actlist);
		timedata = getIntent().getStringExtra("日期");
		uid = getIntent().getStringExtra("uid");
		fid = getIntent().getStringExtra("fid");
		cid = getIntent().getStringExtra("cid");
		init();
		li_activity_show();
		// AddData();
	}

	private void li_activity_show() {
		// Utils.showPrgress(Data_Me_Activity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				list = new ArrayList<Activity_Overview_Model>();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("start", timedata);
				map.put("end", timedata);
				map.put("languageType", com.badibadi.mytools.Dialog
						.getSystemLanguageTypegrzx(Data_Me_Activity.this));
				if (cid != null) {
					map.put("uid", "0");
					map.put("cid", cid);
				}else{
					if(uid!=null && !uid.equals("-1")){
						map.put("uid", uid);
						map.put("cid", "0");
					}else{
						map.put("uid", "0");
						map.put("cid", "0");
					}
				}
				map.put("fid", fid);
//				   
				String t_content = Utils.sendRequest(map, Constants.BadiUrl
						+ "li_activity_show");
				if (t_content != null) {
					//    ("返回期间的活动是:" + t_content);
//					   
//					   
					Results results = Utils.checkResult_NNN(
							getApplicationContext(), t_content);
					if (results != null && !results.getRetmsg().equals("null")) {
						try {
							list = JSONUtils.getListByJsonString(
									results.getRetmsg(),
									Activity_Overview_Model.class);

//							   
							handler.sendEmptyMessage(2);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

	private void init() {
		xListView = (XListView) findViewById(R.id.data_during_xListView1);
		data_during_btn_back = (Button) findViewById(R.id.data_during_btn_back);
		data_during_btn_index = (Button) findViewById(R.id.data_during_btn_index);
		data_during_tital = (TextView) findViewById(R.id.data_during_tital);
		// xListView.setDividerHeight(10);
		xListView.setPadding(10, 0, 10, 10);
		xListView.setXListViewListener(this);
		xListView.setPullLoadEnable(true);
		xListView.setPullRefreshEnable(true);
		data_during_btn_back.setOnClickListener(this);
		data_during_btn_index.setOnClickListener(this);
		Models = new ArrayList<Activity_Overview_Model>();
		String t = null;
		if (timedata != null) {
			t = String.format(getResources().getString(R.string.XXXXActivity),
					timedata);
		} else {
			t = String.format(getResources().getString(R.string.XXXXActivity),
					"12.01.01");
		}
		data_during_tital.setText(t);
	}

	protected void onLoad() {
		// TODO Auto-generated method stub
		xListView.stopRefresh();
		xListView.stopLoadMore();
	}

	private void AddData() {
		// for (int i = 0; i < Constants.temp_num.size(); i++) {
		// Models.add(Constants.overview_Models.get(Constants.temp_num.get(i)));
		// }
		//    ("数据的大小是:" + Models.size());
		x1Adapter = new AfterLandingMyHomePageAllAdapter_1(Models,
				getApplicationContext(), 1, 0.0, 0.0, "2");
		xListView.setAdapter(x1Adapter);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.data_during_btn_index:
			Intent intent = new Intent(Data_Me_Activity.this,
					IndexActivity.class);
			startActivity(intent);
			break;
		case R.id.data_during_btn_back:
			this.finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void onListViewRefresh() {
		// TODO Auto-generated method stub
		handler.postDelayed(new Runnable() {

			@Override
			public void run() {
				onLoad();
			}
		}, 2000);

	}

	@Override
	public void onListViewLoadMore() {
		// TODO Auto-generated method stub
		handler.postDelayed(new Runnable() {

			@Override
			public void run() {
				onLoad();
			}
		}, 2000);
	}
}
